import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_getx/app/data/api/api.dart';
import 'package:flutter_getx/app/modules/group_info/component/edit_group_name_dialog.dart';
import 'package:flutter_getx/app/modules/group_info/component/edit_notice_dialog.dart';
import 'package:flutter_getx/app/service/message_service.dart';
import 'package:flutter_getx/app/service/user_info_service.dart';
import 'package:get/get.dart';

import '../../../data/model/group_info.dart';

class GroupInfoController extends GetxController {
  UserInfoService userInfoService = Get.find();
  MessageService messageService = Get.find();
  Api api = new Api();

  Rx<GroupInfo> groupInfo = new GroupInfo().obs;
  RxList memberList = [].obs;
  
  int groupId = 0;

  final TextEditingController groupNameCtl = new TextEditingController();
  final TextEditingController groupNoticeCtl = new TextEditingController();

  @override
  Future<void> onInit() async {
    print('进入id: ' + Get.arguments.toString() + "的个人profile页面");
    groupId = Get.arguments;
    var res = await api.getGroupInfo(groupId);
    groupInfo.value = GroupInfo.fromJson(res["groupInfo"]);
    memberList.value = res["member"];
    super.onInit();
  }

  toEditGroupName() {
    groupNameCtl.text = groupInfo.value.name!;
    Get.dialog(EditGroupNameDialog(), barrierDismissible: false);
  }

  toEditGroupNotice() {
    groupNoticeCtl.text = groupInfo.value.notice!;
    Get.dialog(EditGroupNoticeDialog(), barrierDismissible: false);
  }

  updateGroupName(String name) async {
    var r = await api.updateGroupName(groupId, name);
    if (r) {
      EasyLoading.showSuccess("更新成功");
      groupInfo.value.name = name;
      groupInfo.refresh();
      Get.back();
    }
  }

  updateGroupNotice(String notice) async {
    var r = await api.updateGroupNotice(groupId, notice);
    if (r) {
      EasyLoading.showSuccess("更新成功");
      groupInfo.value.notice = notice;
      groupInfo.refresh();
      Get.back();
    }
  }


  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {}
}
